$(document).ready(function(){
	createchart1(null);
	createchart2(null);
	var dateTime = createDate();
	/* 初始化日期，默认为今天 */
	createDateTable(dateTime);
	loadList(dateTime);
	$("#remark").focus(function(){
		if($(this).html() == "备注，限20个字符以内（可不填）"){
			$(this).html("");
			$(this).css("color","#000000");
		}
	}).blur(function(){
		if($.trim($(this).html()) == ""){
			$(this).html("备注，限20个字符以内（可不填）");
			$(this).css("color","#cccccc");
		}
	});
	$(".Add").click(function(){
		if(parseInt($("#money_text").val()) == 0){
			popupAlert("请输入价钱，亲。","red");
			return;
		}
		if(parseInt($("#money_text").val()) < 0){
			popupAlert("价钱不能为负数。","red");
			return;
		}
		if($("#remark").val().length > 100){
			popupAlert("备注字符数过多，最多100个字符。","red");
			return;
		}
		if($.trim($("#billTypeName").val()) == ""){
			popupAlert("请选择消费类型，亲。","red");
			return;
		}
		Loadingshow();
		var remark = "";
		if($("#remark").html() != "备注，限20个字符以内（可不填）"){
			remark = $("#remark").html();
		}
		$.ajax({
			type : 'post',
			url : 'bill!save.do',
			data : encodeURI("money="+$("#money_text").val()+"&remark="+remark+"&billTypeName="+$("#billTypeName").val()),
			success : function(msg){
				if(msg == 'success'){
					popupAlert("添加成功。","#000000");
				}else{
					popupAlert("连接服务器失败，请您稍后再试。","red");
				}
			}
		});
	});
});
function loadList(DateTime){
	$("#listLoading").show();
	$("#iframe").hide();
	var iframe = document.getElementById("iframe");   
	iframe.src = "bill!findTodaybyDateTime.do?DateTime="+DateTime+"&date="+new Date();   
	if(iframe.attachEvent){   
		iframe.attachEvent("onload", function(){   
			$("#iframe").show();
			$("#listLoading").hide();
		});
	}else{
		iframe.onload = function(){
			$("#iframe").show();
			$("#listLoading").hide();
		};
	}
}
function createDate(){
	 var d = new Date(); 
	 var year = d.getFullYear();
	 var month = add_zero(d.getMonth()+1); 
	 var day = add_zero(d.getDate());
	 return year+"-"+month+"-"+day;
}
function add_zero(temp){ 
	if(temp<10){
		return "0"+temp; 
	}else {
		return temp;
	}
}
function createDateTable(DateTime){
	$("#DateTableLoading").show();
	$(".Table1,.Table2,.Table3").hide();
	$("#DateTable tr").remove();
	$.ajax({
		type : 'post',
		url : 'bill!findByDateTime.do',
		data : encodeURI("DateTime="+DateTime),
		dataType: "json",
		success : function(msg){
			if(msg.length > 0){
				var html = "<tr>";
				for(var i=0;i<msg.length;i++){
						
					if(i==msg.length-1){
						html += "<td class='selectTd' date='"+msg[i].dateTime+"'><table><tr><th>"+msg[i].day+"</th></tr><tr><td><img src='img/ico_zhi.gif' /> "+msg[i].money+"元</td></tr></table></td>";
						html += "<th><table><tr><th>"+msg[i].year+"年<br>"+msg[i].month+"月</th></tr></table></th></tr>";
					}else{
						html += "<td date='"+msg[i].dateTime+"'><table><tr><th>"+msg[i].day+"</th></tr><tr><td><img src='img/ico_zhi.gif' /> "+msg[i].money+"元</td></tr></table></td>";
					}
				}
				$("#DateTable").append(html);
			}else{
				popupAlert("连接服务器失败，请您稍后再试。","red");
			}
			$("#DateTableLoading").hide();
			$(".Table1,.Table2,.Table3").show();
			$(".Table1 td").each(function(){
				if($(this).find("table").length == 1 && $(this).attr("class") != "selectTd"){
					$(this).mouseover(function(){
						$(this).addClass("selectTd");
					}).mouseout(function(){
						$(this).removeClass("selectTd");
					}).click(function(){
						loadList($(this).attr("date"));
					});
				}
			});
		}
	});
}
function createchart1(date){
	var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'bar',
            backgroundColor: '#f3f3f3'
        },
        title: {
            text: ''
        },
        events:{
        	load:function(){
        		return;
        	}
        },
        subtitle: {
            text: null
        },
        xAxis: {
            categories: ['柴米油盐', '水果零食', '餐饮饮食', '交通通讯', '其他杂项'],
            title: {
                text: null
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: null,
                align: 'high'
            }
        },
        tooltip: {
            formatter: function() {
                return ''+
                    this.x +': '+ this.y +'元';
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -770,
            y: 0,
            floating: true,
            borderWidth: 1,
            backgroundColor: '#FFFFFF',
            shadow: true
        },
        lang: {
        	downloadPNG: '导出PNG图片',
        	downloadJPEG: '导出JPGE图片',
        	downloadPDF: '导出PDF',
        	downloadSVG: '导出SVG',
        	printButtonTitle: '打印',
        	exportButtonTitle: '导出'
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'money',
            data: [30, 30, 30, 30, 30]
        }]
    });
    /*
    setTimeout(function(){
    	chart.series[0].setData([60, 60, 60, 60, 60]);
    	chart.redraw();
    },3000);
    */
}
function createchart2(date){
	var chart = new Highcharts.Chart({
	     chart: {
	         renderTo: 'container2',
	         type: 'column',
	         backgroundColor: '#f3f3f3'
	     },
	     title: {
	         text: ' '
	     },
	     xAxis: {
	         categories: [
	             '本周',
	             '上周',
	             '本月'
	         ],
	         labels: {
	             align: 'center',
	             style: {
	                 fontSize: '13px',
	                 padding : '2px',
	                 fontFamily: 'Verdana, sans-serif'
	             }
	         }
	     },
	     credits: {
	     	enabled:false
	        },
	     yAxis: {
	         min: 0,
	         title: {
	             text: null
	         }
	     },
	     legend: {
	         enabled: false
	     },
	     lang: {
	     	downloadPNG: '导出PNG图片',
	     	downloadJPEG: '导出JPGE图片',
	     	downloadPDF: '导出PDF',
	     	downloadSVG: '导出SVG',
	     	printButtonTitle: '打印',
	     	exportButtonTitle: '导出'
	        },
	     tooltip: {
	         formatter: function() {
	             return '<b>'+ this.x +'</b><br/>'+
	                 '支出'+ Highcharts.numberFormat(this.y, 1) +
	                 '元';
	         }
	     },
	     series: [{
	         name: 'Population',
	         data: [34.5, 21, 20],
	         dataLabels: {
	             enabled: true,
	             rotation: 0,
	             color: '#FFFFFF',
	             align: 'right',
	             x: -3,
	             y: 20,
	             formatter: function() {
	                 return this.y;
	             },
	             style: {
	                 fontSize: '13px',
	                 fontFamily: 'Verdana, sans-serif'
	             }
	         }
	     }]
	});
}
function clearNoNum(obj){
    //先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d.]/g,"");
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g,"");
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g,".");
    //保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}
